//
//  PluginViewController.m
//  HGMMT
//
//  Created by eport_user1 on 2018/4/16.
//  Copyright © 2018年 eport_user1. All rights reserved.
//

#import "PluginViewController.h"
#import "LinkServers.h"
#import "PluginCell.h"
#import "MBProgressHUD+NJ.h"
#import "PluginModel.h"
#import "MJRefresh.h"


static NSString *iden_cell = @"iden_cell";

@interface PluginViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)NSMutableArray *mutaArray;

@end

@implementation PluginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.mutaArray = [NSMutableArray array];
    
    [self initUI];
   // [self loadDate];
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/




-(void)initUI{
 
    self.tableView = [[UITableView alloc]initWithFrame:HG_SCREEN_BOUNDS style:(UITableViewStylePlain)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
   self.tableView.backgroundColor = [UIColor colorWithRed:239/255.0 green:239/255.0 blue:244/255.0 alpha:1];
    self.tableView.tableFooterView = [UIView new];
    [self.view addSubview:self.tableView];
    
    //设置刷新头
   // __weak AppStoreViewController *weakSelf = self;
    
    // 下拉刷新
    self.tableView.mj_header= [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        [self loadDate];
        
    }];
    
    [self.tableView.mj_header beginRefreshing];
    
    
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
 
    return self.mutaArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    PluginCell *cell = [tableView dequeueReusableCellWithIdentifier:iden_cell];
    
    if (cell == nil) {
        
        cell = [[[NSBundle mainBundle]loadNibNamed:@"PluginCell" owner:self options:nil] lastObject];

    }
    cell.model = self.mutaArray[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
 
    return 70;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}


#pragma mark -请求数据
-(void)loadDate{
 
    [MBProgressHUD showMessage:@"正在获取" toView:self.view];
    //发送数据
    NSString *getUserString = @"0006:-L\n";
    NSData *pluginlistData = [getUserString dataUsingEncoding:NSUTF8StringEncoding];
    [[LinkServers shareInstance].clientSocket writeData:pluginlistData withTimeout:-1 tag:0];
    
    
}
#pragma mark -复写父类方法
-(void)getPluginList:(NSArray *)array{
    [super getPluginList:array];
    
    if (array.count > 0) {
        
        [self.mutaArray removeAllObjects];
        
        for (NSDictionary *dic in array) {
            
            PluginModel *model = [[PluginModel alloc]initWithDic:dic];
            [self.mutaArray addObject:model];
        }
    
         [self.tableView reloadData];
    }
    
    [self.tableView.mj_header endRefreshing];

    [MBProgressHUD hideHUDForView:self.view animated:YES];
    

}







@end
